package main

import (
	"fmt"
	"html/template"
	"net/http"
)

//遇事不决，写注释

type User struct {
	Name   string //如果想要在 模版引擎 中使用该字段，则其 首字母 必须是 大写
	Gender string
	Age    int
}

func sayHello(w http.ResponseWriter, r *http.Request) {
	//2.解析模版
	t, err := template.ParseFiles("web04/f.tmpl") //请勿刻舟求剑
	if err != nil {
		fmt.Printf("Parse template failed, err:%v\n", err)
		return
	}
	//3.渲染模版
	//结构体类型
	u1 := User{
		Name:   "小王子",
		Gender: "男",
		Age:    18,
	}
	//map类型
	//首字母不需要大写，通过 key值 来获取数据
	m1 := map[string]interface{}{
		"name":   "小王子",
		"gender": "男",
		"age":    18,
	}
	hobbyList := []string{
		"篮球",
		"足球",
		"双色球",
	}
	err = t.Execute(w, map[string]interface{}{
		"u1":    u1,
		"m1":    m1,
		"hobby": hobbyList,
	})
	if err != nil {
		fmt.Printf("render template failed, err:%v\n", err)
	}
}

func main() {
	http.HandleFunc("/", sayHello)
	err := http.ListenAndServe(":9000", nil)
	if err != nil {
		fmt.Printf("HTTP server start failed, err:%v\n", err)
		return
	}
}
